﻿namespace WMS6.Web.TaskMgmt
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TaskMgtServiceRef;

    public class Task : BasePage
    {
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnSave;
        protected DropDownList ddlPriority;
        protected DropDownList ddlStatus;
        protected DropDownList ddlTaskType;
        protected DropDownList ddlUserPosition;
        protected HtmlForm form1;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnTaskDetailID;
        protected HtmlHead Head1;
        protected HtmlImage img;
        protected HtmlImage img5;
        protected HtmlImage img6;
        protected HtmlImage img7;
        protected Label lblCaseID;
        protected Label lblCreatedDate;
        protected Label lblDocID;
        protected Label lblEndTime;
        protected Label lblFinalToLoc;
        protected Label lblFromLoc;
        protected Label lblFromLpn;
        protected Label lblHoldID;
        protected Label lblListID;
        protected Label lblLogicalFromLoc;
        protected Label lblLogicalToLoc;
        protected Label lblLot;
        protected Label lblMsg;
        protected Label lblOrderID;
        protected Label lblOrderLineID;
        protected Label lblOwnerID;
        protected Label lblPickDetailID;
        protected Label lblPickMethod;
        protected Label lblPriority;
        protected Label lblQty;
        protected Label lblReasonID;
        protected Label lblSeqNo;
        protected Label lblSkuID;
        protected Label lblSourcePriority;
        protected Label lblSourceType;
        protected Label lblStartTime;
        protected Label lblStatus;
        protected Label lblStatusMsg;
        protected Label lblTask;
        protected Label lblTaskDetailID;
        protected Label lblTaskType;
        protected Label lblToLoc;
        protected Label lblToLpn;
        protected Label lblUomID;
        protected Label lblUomQty;
        protected Label lblUserID;
        protected Label lblUserIDOverride;
        protected Label lblUserPosition;
        protected Label lblWaveID;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected Label one4;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCaseID;
        protected TextBox txtCreatedDate;
        protected TextBox txtDocID;
        protected TextBox txtEndTime;
        protected TextBox txtFinalToLoc;
        protected TextBox txtFromLoc;
        protected TextBox txtFromLpn;
        protected TextBox txtHoldID;
        protected TextBox txtListID;
        protected TextBox txtLogicalFromLoc;
        protected TextBox txtLogicalToLoc;
        protected TextBox txtLot;
        protected TextBox txtOrderID;
        protected TextBox txtOrderLineID;
        protected TextBox txtOwnerID;
        protected TextBox txtPickDetailID;
        protected TextBox txtPickMethod;
        protected TextBox txtQty;
        protected TextBox txtReasonID;
        protected TextBox txtSeqNo;
        protected TextBox txtSkuID;
        protected TextBox txtSourcePriority;
        protected TextBox txtSourceType;
        protected TextBox txtStartTime;
        protected TextBox txtStatusMsg;
        protected TextBox txtTaskDetailID;
        protected TextBox txtToLoc;
        protected TextBox txtToLpn;
        protected TextBox txtUomID;
        protected TextBox txtUomQty;
        protected TextBox txtUserID;
        protected TextBox txtUserIDOverride;
        protected TextBox txtWaveID;
        protected UpdatePanel up1;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtTaskDetailID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F6050_001");
            }
            else
            {
                List<string> taskDetailID = new List<string> {
                    this.txtTaskDetailID.Text
                };
                try
                {
                    this.lblMsg.Text = TaskMgtServiceClientProxy.GetInstance().DeleteTaskDetail(taskDetailID) + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                    this.lblMsg.Visible = true;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("TaskQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("TaskList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            this.txtTaskDetailID.Text = string.Empty;
            this.ddlTaskType.SelectedValue = string.Empty;
            this.txtStartTime.Text = string.Empty;
            this.ddlStatus.SelectedValue = string.Empty;
            this.txtUserID.Text = string.Empty;
            this.txtEndTime.Text = string.Empty;
            this.ddlPriority.SelectedValue = string.Empty;
            this.txtUserIDOverride.Text = string.Empty;
            this.txtCreatedDate.Text = string.Empty;
            this.txtReasonID.Text = string.Empty;
            this.txtStatusMsg.Text = string.Empty;
            this.txtOwnerID.Text = string.Empty;
            this.txtFromLoc.Text = string.Empty;
            this.txtToLoc.Text = string.Empty;
            this.txtSkuID.Text = string.Empty;
            this.txtFromLpn.Text = string.Empty;
            this.txtToLpn.Text = string.Empty;
            this.txtUomID.Text = string.Empty;
            this.txtLogicalFromLoc.Text = string.Empty;
            this.txtLogicalToLoc.Text = string.Empty;
            this.txtQty.Text = string.Empty;
            this.txtLot.Text = string.Empty;
            this.txtFinalToLoc.Text = string.Empty;
            this.txtUomQty.Text = string.Empty;
            this.txtOrderID.Text = string.Empty;
            this.txtCaseID.Text = string.Empty;
            this.txtOrderLineID.Text = string.Empty;
            this.txtPickDetailID.Text = string.Empty;
            this.txtPickMethod.Text = string.Empty;
            this.txtDocID.Text = string.Empty;
            this.txtWaveID.Text = string.Empty;
            this.txtSourcePriority.Text = string.Empty;
            this.txtHoldID.Text = string.Empty;
            this.txtSourceType.Text = string.Empty;
            this.txtListID.Text = string.Empty;
            this.ddlUserPosition.SelectedValue = string.Empty;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.hdnFlag.Value))
                {
                    this.InsertTaskInfo();
                }
                else
                {
                    this.UpdateTaskInfo();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private TaskDetailInfo GetTaskDetailInfo()
        {
            //TaskDetailInfo taskDetailQuerEntity;
            return new TaskDetailInfo
                       {
                           TaskDetailID = this.txtTaskDetailID.Text,
                           TaskType = this.ddlTaskType.SelectedValue,
                           StartTime = DateTime.Parse(this.txtStartTime.Text),
                           Status = this.ddlStatus.SelectedValue,
                           UserID = this.txtUserID.Text,
                           EndTime = DateTime.Parse(this.txtEndTime.Text),
                           Priority = this.ddlPriority.SelectedValue,
                           UserIdOverride = this.txtUserIDOverride.Text,
                           ReleaseDate = DateTime.Parse(this.txtCreatedDate.Text),
                           ReasonID = this.txtReasonID.Text,
                           StatusMsg = this.txtStatusMsg.Text,
                           OwnerID = this.txtOwnerID.Text,
                           FromLoc = this.txtFromLoc.Text,
                           ToLoc = this.txtToLoc.Text,
                           SkuID = this.txtSkuID.Text,
                           FromLpnID = this.txtFromLpn.Text,
                           ToLpnID = this.txtToLpn.Text,
                           UomID = this.txtUomID.Text,
                           LogicalFromLoc = this.txtLogicalFromLoc.Text,
                           LogicalToLoc = this.txtLogicalToLoc.Text,
                           Qty = decimal.Parse(this.txtQty.Text),
                           Lot = this.txtLot.Text,
                           FinalToLoc = this.txtFinalToLoc.Text,
                           UomQty = decimal.Parse(this.txtUomQty.Text),
                           OrderID = this.txtOrderID.Text,
                           CaseID = this.txtCaseID.Text,
                           OrderLineID = int.Parse(this.txtOrderLineID.Text),
                           SeqNo = int.Parse(this.txtSeqNo.Text),
                           PickDetailID = this.txtPickDetailID.Text,
                           PickMethod = this.txtPickMethod.Text,
                           DocID = this.txtDocID.Text,
                           WaveID = this.txtWaveID.Text,
                           SourcePriority = this.txtSourcePriority.Text,
                           HoldID = this.txtHoldID.Text,
                           SourceType = this.txtSourceType.Text,
                           ListID = this.txtListID.Text,
                           UserPosition = this.ddlUserPosition.SelectedValue,
                           Message01 = "",
                           Message02 = "",
                           Message03 = "",
                           CreatedBy = base.UserInfo.UserID,
                           UpdatedBy = base.UserInfo.UserID
                       };
        }

        private void InsertTaskInfo()
        {
            try
            {
                string strTask = string.Empty;
                strTask = TaskMgtServiceClientProxy.GetInstance().InsertTaskDetail(this.GetTaskDetailInfo());
                if (!string.IsNullOrEmpty(strTask))
                {
                    this.hdnTaskDetailID.Value = strTask;
                    this.lblMsg.Text = strTask + base.GetGlobalMsgResource("C004");
                    this.lblMsg.Visible = true;
                    this.txtTaskDetailID.Text = strTask;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlPriority, "TaskPriority");
            this.ddlPriority.SelectedValue = "5";
            base.BindData2DDLBySysCode(this.ddlStatus, "TaskStatus");
            base.BindData2DDLBySysCode(this.ddlTaskType, "TaskType");
            base.BindData2DDLBySysCode(this.ddlUserPosition, "UserPosition");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnTaskDetailID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnFlag.Value = "1";
                this.SetTaskinfoForm();
                this.img.Disabled = true;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTaskinfoForm()
        {
            try
            {
                string taskDetailID = this.hdnTaskDetailID.Value;
                TaskDetailInfo taskDetailQuerEntity = TaskMgtServiceClientProxy.GetInstance().GetTaskDetailByID(taskDetailID);
                base.PageQueryEntitySession = taskDetailQuerEntity;
                if (taskDetailQuerEntity != null)
                {
                    if (taskDetailQuerEntity.Status.ToUpper() == "Completed".ToUpper())
                    {
                        this.TextEnabled("Completed");
                    }
                    this.txtTaskDetailID.Text = taskDetailQuerEntity.TaskDetailID;
                    this.ddlTaskType.SelectedValue = taskDetailQuerEntity.TaskType;
                    this.txtStartTime.Text = base.GetPreferenceShortDate(taskDetailQuerEntity.StartTime);
                    this.ddlStatus.SelectedValue = taskDetailQuerEntity.Status;
                    this.txtUserID.Text = taskDetailQuerEntity.UserID;
                    this.txtEndTime.Text = base.GetPreferenceShortDate(taskDetailQuerEntity.EndTime);
                    this.ddlPriority.SelectedValue = taskDetailQuerEntity.Priority;
                    this.txtUserIDOverride.Text = taskDetailQuerEntity.UserIdOverride;
                    this.txtCreatedDate.Text = base.GetPreferenceShortDate(taskDetailQuerEntity.CreatedDate);
                    this.txtReasonID.Text = taskDetailQuerEntity.ReasonID;
                    this.txtStatusMsg.Text = taskDetailQuerEntity.StatusMsg;
                    this.txtOwnerID.Text = taskDetailQuerEntity.OwnerID;
                    this.txtFromLoc.Text = taskDetailQuerEntity.FromLoc;
                    this.txtToLoc.Text = taskDetailQuerEntity.ToLoc;
                    this.txtSkuID.Text = taskDetailQuerEntity.SkuID;
                    this.txtFromLpn.Text = taskDetailQuerEntity.FromLpnID;
                    this.txtToLpn.Text = taskDetailQuerEntity.ToLpnID;
                    this.txtUomID.Text = taskDetailQuerEntity.UomID;
                    this.txtLogicalFromLoc.Text = taskDetailQuerEntity.LogicalFromLoc;
                    this.txtLogicalToLoc.Text = taskDetailQuerEntity.LogicalToLoc;
                    this.txtQty.Text = base.GetPreferenceDigit(taskDetailQuerEntity.Qty);
                    this.txtLot.Text = taskDetailQuerEntity.Lot;
                    this.txtFinalToLoc.Text = taskDetailQuerEntity.FinalToLoc;
                    this.txtUomQty.Text = base.GetPreferenceDigit(taskDetailQuerEntity.UomQty);
                    this.txtOrderID.Text = taskDetailQuerEntity.OrderID;
                    this.txtCaseID.Text = taskDetailQuerEntity.CaseID;
                    this.txtOrderLineID.Text = taskDetailQuerEntity.OrderLineID.ToString();
                    this.txtPickDetailID.Text = taskDetailQuerEntity.PickDetailID;
                    this.txtPickMethod.Text = taskDetailQuerEntity.PickMethod;
                    this.txtDocID.Text = taskDetailQuerEntity.DocID;
                    this.txtWaveID.Text = taskDetailQuerEntity.WaveID;
                    this.txtSourcePriority.Text = taskDetailQuerEntity.SourcePriority;
                    this.txtHoldID.Text = taskDetailQuerEntity.HoldID;
                    this.txtSourceType.Text = taskDetailQuerEntity.SourceType;
                    this.txtListID.Text = taskDetailQuerEntity.ListID;
                    this.ddlUserPosition.SelectedValue = taskDetailQuerEntity.UserPosition;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void TextEnabled(string Type)
        {
            string CS40000 = Type;
            if ((CS40000 != null) && (CS40000 == "Completed"))
            {
                this.txtTaskDetailID.Enabled = false;
                this.ddlTaskType.Enabled = false;
                this.txtStartTime.Enabled = false;
                this.ddlStatus.Enabled = false;
                this.txtUserID.Enabled = false;
                this.txtEndTime.Enabled = false;
                this.ddlPriority.Enabled = false;
                this.txtUserIDOverride.Enabled = false;
                this.txtCreatedDate.Enabled = false;
                this.txtReasonID.Enabled = false;
                this.txtStatusMsg.Enabled = false;
                this.txtOwnerID.Enabled = false;
                this.txtFromLoc.Enabled = false;
                this.txtToLoc.Enabled = false;
                this.txtSkuID.Enabled = false;
                this.txtFromLpn.Enabled = false;
                this.txtToLpn.Enabled = false;
                this.txtUomID.Enabled = false;
                this.txtLogicalFromLoc.Enabled = false;
                this.txtLogicalToLoc.Enabled = false;
                this.txtQty.Enabled = false;
                this.txtLot.Enabled = false;
                this.txtFinalToLoc.Enabled = false;
                this.txtUomQty.Enabled = false;
                this.txtOrderID.Enabled = false;
                this.txtCaseID.Enabled = false;
                this.txtOrderLineID.Enabled = false;
                this.txtSeqNo.Enabled = false;
                this.txtPickDetailID.Enabled = false;
                this.txtPickMethod.Enabled = false;
                this.txtDocID.Enabled = false;
                this.txtWaveID.Enabled = false;
                this.txtSourcePriority.Enabled = false;
                this.txtHoldID.Enabled = false;
                this.txtSourceType.Enabled = false;
                this.txtListID.Enabled = false;
                this.ddlUserPosition.Enabled = false;
                this.btnDelete.Enabled = false;
                this.btnSave.Enabled = false;
            }
        }

        private void UpdateTaskInfo()
        {
            try
            {
                string strTask = string.Empty;
                strTask = TaskMgtServiceClientProxy.GetInstance().UpdateTaskDetail(this.GetTaskDetailInfo());
                if (!string.IsNullOrEmpty(strTask))
                {
                    this.hdnTaskDetailID.Value = strTask;
                    this.lblMsg.Text = strTask + base.GetGlobalMsgResource("C002");
                    this.lblMsg.Visible = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }
    }
}

